#ifndef MESSAGE_H
#define MESSAGE_H


#include <QString>
#include <QJsonObject>
// 消息类型枚举
enum MessageType {
    PRIVATE_CHAT,
    GROUP_CHAT,
    LOGIN_REQUEST,
    REGISTER_REQUEST,
    LOGIN_RESPONSE,
    REGISTER_RESPONSE
};

// 消息结构体
struct Message {
    MessageType type;
    QString sender;
    QString receiver;
    QString dataVal;
    bool success;


    // 将 Message 结构体转换为 JSON 对象
    QJsonObject toJson() const {
        QJsonObject json;
        json["type"] = static_cast<int>(type);
        json["sender"] = sender;
        json["receiver"] = receiver;
        json["dataVal"] = dataVal;
        json["success"] = success;
        return json;
    }

    // 从 JSON 对象中解析出 Message 结构体
    static Message fromJson(const QJsonObject &json) {
        Message message;
        message.type = static_cast<MessageType>(json["type"].toInt());
        message.sender = json["sender"].toString();
        message.receiver = json["receiver"].toString();
        message.dataVal = json["dataVal"].toString();
        message.success = json["success"].toBool();
        return message;
    }
};

#endif // MESSAGE_H
